<?php
require_once '../global.inc.php';
Database::connect($HOST, $NAME, $PWD, $DB);
$table 			= "member_activity";
// operation操作类型：add、del、query、edit、join
$operation 		= $_GET["op"];


//成员加入活动前数据提取校验
if ($operation=="join") {
	$memberId				= $_GET["id"];
	$memberActivityArray 	= $_SESSION["memberActivityArray"];
	foreach ($memberActivityArray as $memberActivity) {
		if ($memberActivity["member_id"]==$memberId) {
			echo "<script>alert('该会员已经加入了本次活动！');</script>";
			echo "<script>window.location.href='../view/member/memberQuery.php?tag=j';</script>";
		}
	}

	//查询该用户资料
	$memberInfo = Database::findById("member", "member_id", "$memberId");

	$_SESSION["memberInfo"]	= $memberInfo;
	
	//转向处理页面
	echo "<script>window.location.href='../view/activity/instanceMemberDetail.php?op=add';</script>";
}

//活动成员新增
if ($operation=="add") {
	$instanceId		= $_POST["instanceId"];
	$memberId		= $_POST["memberId"];
	$payFee			= $_POST["payFee"];
	$discountFee	= $_POST["discountFee"];
	$payScore		= $_POST["payScore"];
	$comments		= $_POST["comments"];
	$status			= 1; //0:活动取消  1:活动生成
	Database::begin();

	//插入成员信息
	$sql = "INSERT INTO member_activity(instance_id,member_id,pay_fee,discount_fee,pay_score,comments,status)"
		  ."VALUES ('$instanceId','$memberId','$payFee','$discountFee','$payScore','$comments','$status')";
	Database::insert($sql);

	Database::commit();
	Database::end();
	//转向展示页面
	echo "<script>alert('添加成功！');window.close();</script>";
	//echo "<script>window.location.href='instanceAction.php?op=get&tag=e&id=$instanceId'</script>";
}
//活动成员删除
if ($operation=="del") {
	$instanceId		= $_SESSION["instanceInfo"]["instance_id"];
	$memberIds		= $_GET["id"];
	Database::begin();

	//插入活动模板资料
	$sql = "delete from member_activity where instance_id=$instanceId and member_id in ($memberIds)";
			Database::delete($sql);

			Database::commit();
			Database::end();
			//转向展示页面
	echo "<script>alert('删除成功！');window.close();</script>";
	//echo "<script>window.location.href='instanceAction.php?op=get&tag=e&id=$instanceId'</script>";
}
//获取成员的当前信息
if ($operation=="get") {
	$instanceId		= $_SESSION["instanceInfo"]["instance_id"];
	$memberId		= $_GET["id"];

	//查询会员信息
	$memberInfo		= Database::findById("member", "member_id", $memberId);


	$sql			= "select * from member_activity where instance_id=$instanceId and member_id=$memberId ";
	$instanceMemberArray = Database::find($sql);
	$instanceMemberInfo	 = $instanceMemberArray[0];
	
	//将取得的信息写入session
	$_SESSION["memberInfo"]	= $memberInfo;
	$_SESSION["instanceMemberInfo"] = $instanceMemberInfo;
	
	echo "<script>window.location.href='../view/activity/instanceMemberDetail.php?op=edit&id=$memberId'</script>";
}
//活动成员修改
if ($operation=="edit") {
	$instanceId		= $_POST["instanceId"];
	$memberId		= $_POST["memberId"];
	$payFee			= $_POST["payFee"];
	$discountFee	= $_POST["discountFee"];
	$score			= $_POST["score"];
	$payScore		= $_POST["payScore"];
	$comments		= $_POST["comments"];		
	Database::begin();

	//插入活动模板资料
	$sql = "update member_activity set
				pay_fee		= '$payFee',
				discount_fee= '$discountFee',
				pay_score 	= '$payScore',
				comments	= '$comments'";
	$sql .= " where instance_id=$instanceId and member_id = $memberId";
	Database::update($sql);

	Database::commit();
	Database::end();
	//转向展示页面
	echo "<script>alert('更新成功！');window.close();</script>";
	//echo "<script>window.location.href='instanceAction.php?op=get&tag=e&id=$instanceId'</script>";
}

//关闭数据库
Database::close();
?>